﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum MessageType
{
    Weather = 0,
    IsStart,
    Event,
    Update,
    Action,
    DriveError
}

public class NetManager : MonoBehaviour
{
    public static NetManager _instance;
    
    private Dictionary<MessageType, ControllerBase> ControllerDic = new Dictionary<MessageType, ControllerBase>(); 

    public string IP;
    public int port;
    public bool IsServer;

    private void Awake()
    {
        _instance = this;

        NetController.GetInstance().Init(IsServer, IP, port);
    }

    /// <summary>
    /// 拆包后，将内容发送给相应的控制器
    /// </summary>
    /// <param name="type"></param>
    /// <param name="message"></param>
    public void HandleMessage(short type,object message)
    {
        ControllerDic[(MessageType)type].TranslateObj(message);
    }

    public void RegistController(MessageType type, ControllerBase controller)
    {
        ControllerDic.Add(type,controller);
    }

    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="type">消息类型</param>
    /// <param name="message">消息内容</param>
    public void SendToIpad<T>(MessageType type,T message)
    {
        NetController.GetInstance().SendMessage(type,message);
    }
}
